import {throttle} from './util.js'
const uniRequest = (params, method, url, loading, contentType) => {
  return new Promise((resolve, reject) => {
	  let data = params || {};
	  let currentLang = uni.getStorageSync('CURRENT_LANG')
	  currentLang = currentLang == 'en' ? 'en-US':'zh-CN'

	  if (!loading){
	        wx.showLoading({ mask: true, title: '加载中' })
	      }
	  uni.request({
	  	url,
		method:method || {},
		data:data,
		header:{
			'Content-Type': contentType ? 'application/json' : 'application/x-www-form-urlencoded',
			'X-Access-Token': uni.getStorageSync('token') || '',
			'Accept-Language': currentLang
		},
		success(res) {
			  console.log('success',res);
			 setTimeout(()=>{
			  if (!loading) {
				wx.hideLoading()
			  }
			  if (res.statusCode === 200 && res.data.code == 200 ){
				resolve(res.data);
			  }else{
				 console.log('errRes',res);
				if (res.statusCode == 500) {
					
					getApp().globalData.AppLogin = false
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/loginOne/loginOne'
						})	
					},500)
					
					
				} else{
					
					let message = res.data.message
					if (message) {
						
						if (message.includes("操作失败")) {
							message = '请求失败'
						}
						
					} else{
						message = '请求失败'
					}
					
					wx.showToast({
					  title: message,
					  icon: 'none',
					  duration: 2000
					})
				}
				
				
				
				reject(res.data)
			  }
			},500)
		},
		fail(err){
			console.log('err',err);
			
			
		        setTimeout(()=>{
		          if (!loading) {
		            wx.hideLoading()
		          }
				  
		          wx.showToast({
		            title: '服务器出错~',
		            icon: 'cancel',
		            duration: 2000
		          })
		          reject(err)
		        },1000)
		      }
	  })  
  })
}
gotoLogin:throttle(function(){
	
			
		
},1500)
export default uniRequest;